<template>
  <template v-for="(item,index) in props.menuData">
    <!-- 如果没有子目录的 -->
    <el-menu-item @click="handleClick(item, `${props.index}-${item.meta.id}`)"
      v-if="!item.children||item.children.length==0" :index="`${props.index}-${item.meta.id}`"
      :key='`${props.index}-${item.meta.id}`'>
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon>
      <span>{{item.meta.name}}</span>
    </el-menu-item>
    <!-- 有子目录的 -->
    <el-sub-menu v-else :index="`${props.index}-${item.meta.id}`">
      <template #title>
        <el-icon size="20">
          <component :is="item.meta.icon"></component>
        </el-icon>
        <span>{{item.meta.name}}</span>
      </template>
      <!-- 递归组件   -->
      <treeMenu :index="`${props.index}-${item.meta.id}`" :menuData="item.children"></treeMenu>
    </el-sub-menu>
  </template>


</template>

<script setup>
  const props = defineProps(['menuData', 'index'])
  // console.log(props);

  // 实现路由跳转
  import { useRouter } from 'vue-router'
  import { useStore } from 'vuex'
  // 创建实例
  const userouter = useRouter()
  const store = useStore()
  // 点击菜单
  const handleClick = (item, active) => {
    // console.log(item);
    store.commit('addMenu', item.meta)
    userouter.push(item.meta.path)
    store.commit('updateMenuActive', active)
  }


</script>

<style lang="less" scoped>


</style>